<template>
  <span :gpEndTime="gpEndTime" :endText="endText">
    <slot>
      {{content}}
    </slot>
  </span>
</template>
<script>
  /* eslint-disable comma-dangle,semi */

  export default {
    data () {
      return {
        content: '',
      }
    },
    props: {
      gpEndTime: {
        type: String,
        default: ''
      },
      endText: {
        type: String,
        default: ''
      }
    },
    methods: {
      countdown (timestamp) {
//          alert(timestamp);
        let self = this;
//        timestamp = localStorage.getItem('gpEndTime');
        let time = Number.parseInt(timestamp); // 钩子函数中不能JSON input
        let timer = setInterval(function () {
          let nowTime = new Date().getTime();
          let gpEndTime = time;
          let t = (gpEndTime - nowTime) / 1000;
          if (t > 0) {
            let day = Math.floor(t / (24 * 60 * 60));
            let hour = Math.floor((t / 3600) % 24);
            let min = Math.floor((t / 60) % 60);
            let sec = Math.floor(t % 60);
            hour = hour < 10 ? '0' + hour : hour;
            min = min < 10 ? '0' + min : min;
            sec = sec < 10 ? '0' + sec : sec;
            let format = '';
            if (day > 0) {
              format = `${day}:${hour}:${min}:${sec}`;
            }
            if (day <= 0 && hour > 0) {
              format = `${hour}:${min}:${sec}`;
            }
            if (day <= 0 && hour <= 0) {
              format = `${min}:${sec}`;
            }
            self.content = '剩余'+format;
          } else {
            clearInterval(timer);
            self.content = self.endText;
//            self._callback();
//            this.$emit('change', 'disabled');
          }
        }, 1000);
      },
//      _callback () {
//        if (this.callback && this.callback instanceof Function) {
//          this.callback(...this);
//        }
//      }
    },
    mounted () {
//      alert(this.gpEndTime + '----子组件');
      this.countdown(this.gpEndTime);
    }
//    watch: {
//      gpEndTime (curVal, oldVal) {
//        alert(curVal, oldVal);
//      }
//    }
  }
</script>
